#include "MLNBAStatDataLoader.h"

const wchar_t* CMLNBAStatDataLoader::_PLAYER_REASON_SCHEMA[]= {
	L"ilkid",
	L"year",
	L"firstname",
	L"lastname",
	L"team",
	L"leag",
	L"gp",
	L"minutes",
	L"pts",
	L"oreb",
	L"dreb",
	L"reb",
	L"asts",
	L"stl",
	L"blk",
	L"turnover",
	L"pf",
	L"fga",
	L"fgm",
	L"fta",
	L"ftm",
	L"tpa",
	L"tpm",
	L"\0",
};

CMLNBAStatDataLoader::CMLNBAStatDataLoader(void)
{
	m_nStartYear = -1;
	m_nEndYear = -1;
}

CMLNBAStatDataLoader::~CMLNBAStatDataLoader(void)
{
}

int CMLNBAStatDataLoader::Initialize(void)
{
	m_nStartYear = -1;
	m_nEndYear = -1;
	return 0;
}

int CMLNBAStatDataLoader::SetYearRange(int nStartYear, int nEndYear)
{
	m_nStartYear = nStartYear;
	m_nEndYear = nEndYear;
	ML_ASSERT(m_nEndYear >= m_nStartYear);
	return 0;
}

int CMLNBAStatDataLoader::LoadPlayerRegularSeason(const std::wstring& wstrFilename, std::vector<PlayerSeasonData>& playerSet)
{
	std::wifstream fileStream(wstrFilename.c_str());
	if (fileStream.fail())
	{
		std::wcerr<<L"Cannot open this file : "<<wstrFilename<< L" !\n";
		ML_ASSERT(false);
		return -1;
	}
	// skip the first line
	wchar_t ch;
	while((ch = fileStream.get()) != '\n') {};
	// Read lines
	wchar_t line[1024*10];
	PlayerSeasonData playerData;
	while(fileStream.eof() == false)
	{
		fileStream.getline(line, sizeof(line));
		LoadPlayerSeasonData(line, playerData);
		
		// Filter by the year
		if (m_nStartYear >= 0)
		{
			if (playerData.year < m_nStartYear)
				continue;
		}
		if (m_nEndYear >= 0)
		{
			if (playerData.year > m_nEndYear)
				break;
		}

		playerSet.push_back(playerData);
	}
	// close file
	fileStream.close();
	return 0;
}

int CMLNBAStatDataLoader::LoadPlayerSeasonData(const wchar_t* srcline, PlayerSeasonData& player)
{
	const wchar_t* p = srcline;
	ReadStr(player.ilkid, p);
	ReadDouble(player.year, p);
	ReadStr(player.firstname, p);
	ReadStr(player.lastname, p);
	ReadStr(player.team, p);
	ReadStr(player.leag, p);
	ReadDouble(player.gp, p);
	ReadDouble(player.minutes, p);
	ReadDouble(player.pts, p);
	ReadDouble(player.oreb, p);
	ReadDouble(player.dreb, p);
	ReadDouble(player.reb, p);
	ReadDouble(player.asts, p);
	ReadDouble(player.stl, p);
	ReadDouble(player.blk, p);
	ReadDouble(player.turnover, p);
	ReadDouble(player.pf, p);
	ReadDouble(player.fga, p);
	ReadDouble(player.fgm, p);
	ReadDouble(player.fta, p);
	ReadDouble(player.ftm, p);
	ReadDouble(player.tpa, p);
	ReadDouble(player.tpm, p);
	return 0;
}
